<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/26
 * Time: 21:01
 */
namespace app\models;

use think\Model;

/**
 * Class AuthModel
 * @package app\models
 */
class AuthModel extends Model
{
    protected $table = 'edu_auth_group';

    /**
     * @param $id
     * @param string $type
     * @return AuthGroupAccessModel|array|null
     * @throws \think\exception\DbException
     */
    public static function getById($id,$type = 'all')
    {
        if(!is_numeric($id)) {
            return [];
        }
        $auth_group = self::get(['id'=>$id]);
        if($auth_group) {
            return (isset($auth_group[$type])) ?  $auth_group[$type] : $auth_group;
        }
        return [];
    }
    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        if($where){
            $result =self::where($where)->order($order)->paginate($limit,false,['page'=>$page]);
        }else{
            $result =self::order($order)->paginate($limit,false,['page'=>$page]);
        }
        if(!$result->total()){
            return ['code' => 0, 'msg' => "ok", 'count' =>0, 'data' => []];
        }
        foreach ($result as $key => $value) {
            $result[$key]['status_text'] = $value->status_text;
            $result[$key]['type_text'] = $value->type_text;

            if($value['group_type'] == 1){
                $result[$key]['group_type_text'] = '一般类属性';
            }else if($value['group_type'] == 2){
                $result[$key]['group_type_text'] = '老师类属性';
            }else if($value['group_type'] == 3){
                $result[$key]['group_type_text'] = '助教类属性';
            }else if($value['group_type'] == 4){
                $result[$key]['group_type_text'] = '班主任/学管师类属性';
            }else if($value['group_type'] == 5){
                $result[$key]['group_type_text'] = '教务类属性';
            }
            if($value['company_id'] == null){
                $result[$key]['company_name'] = '万学网络科技';
            }else{
                $result[$key]['company_name'] = CompanyModel::get($value['company_id'])['company_name'];
            }

        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }

    /**
     * 状态
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStatusTextAttr($value, $data)
    {
        $status = [1 => "启用中", 0 => "禁用中"];
        return $status[$data['status']];
    }

	/**
	 * 类型
	 * @param $value
	 * @param $data
	 * @return mixed
	 */
	public function getTypeTextAttr($value, $data)
	{
		$status = [1 => "超管", 2 => "系统", 3 => "区域", 4 => "校区", 5 => "员工"];
		return $status[$data['type']];
	}

    /**
     * 角色分配权限
     * @param $rule_ids
     * @param $role_id
     * @return AuthModel
     */
    public function addAuthRule($rule_ids,$role_id)
    {
        $where = array(
            'id' => $role_id,
        );
        $data = array(
            'rules' => $rule_ids,
        );
        return $this->where($where)->update($data);
    }
}